// pages/result/result.js
/**
 * 所有结果的请求都在该页面完成
 */
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
let Request = require("../../utils/ajax"); //导入模块
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '**',//用户名称
        truename:"",//完整的姓名
        nuber: '******************',
        time: '',//获取登陆时间
        encodeData: '',
        luminance: 0.5,//设置屏幕亮度
        phone: '',//手机号
        personNumber: '',//身份证号
        codeStr: '',//根据码色设置页面整体的图片
        qrcodeImg: '',
        vaccinationInfos: [],
        nucleicAcidInfos: [],//核酸检测信息
        timeCode: '',
        newdate:"",
        ke:5,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setScreenBrightness(1)
        this.writeCurrentDate()
        let data = JSON.parse(decodeURIComponent(options.data))

        if (data.data.name) {
            this.setData({
                truename:data.data.name,
            })
            if (data.data.name.length > 3) {
                data.data.name = this.changeStr(data.data.name, 0, '*')
                data.data.name = this.changeStr(data.data.name, 1, '*')
            } else {
                data.data.name = this.changeStr(data.data.name, 0, '*')
            }
        } else {
            data.data.name = '您'
        }


        this.setData({
            personNumber: data.data.personNumber,
            codeStr: data.data.codeStr,
            name: data.data.name,
            encodeData: data.data.encodeData,
            luminance: options.luminance,
            phone: data.data.phone,
            value: data.data.value
        })

    },
    setScreenBrightness(e) {
        wx.setScreenBrightness({
            value: e,    //屏幕亮度值，范围 0~1，0 最暗，1 最亮
        })
    },
    getXgAndHsData() {
        let that = this
        console.log(that.data.personNumber);
        console.log(that.data.name);
        Request.get('/weChat/getXgAndHsData', //调用方法
            { personNumber: that.data.personNumber ,
                name:that.data.truename},
            {
                'content-type': 'application/json', // 默认值
            },
        ).then(res => { //成功回调
            if (res.data.success == true) {
                console.log(res.data.data);
                that.setData({
                    nucleicAcidInfos: res.data.data.nucleicAcidInfos ? res.data.data.nucleicAcidInfos : [],
                    vaccinationInfos: res.data.data.vaccinationInfos ? res.data.data.vaccinationInfos : [],
                })
                if (res.data.data.nucleicAcidInfos) {
                    let sTime = Date.parse(new Date(res.data.data.nucleicAcidInfos[0].receivetime))
                    let eTime = Date.parse(new Date());
                    //作为除数的数字
                    var divNumHour = 1000 * 3600;
                    const hour = parseInt((eTime - sTime) / parseInt(divNumHour))
                    if (hour <= 48) {
                        that.setData({
                            timeCode: '48小时'
                        })
                    } else if (hour > 48 && hour <= 72) {
                        that.setData({
                            timeCode: '72小时'
                        })
                    } else {
                        that.setData({
                            timeCode: ''
                        })
                    }
                }
            }
        }).catch(err => { }) //异常回调
    },
    
    // 解除绑定，页面返回，并通知后台
    unbindIdentity: function () {
        let that = this
        Request.post('/weChat/unbindIdentity?phone=' + that.data.phone, //调用方法
            {
                'content-type': 'application/json', // 默认值
            },
        ).then(res => { //成功回调
            if (res.data.success == true) {
                wx.redirectTo({
                    url: '/pages/index/index?isUnbind=1',
                  })
                wx.showToast({
                    title: '解绑成功',
                    icon: 'none',
                    duration: 2000
                })
            }
        }).catch(err => { }) //异常回调
    },
    // 切换手机号
    switchphone:function(){
        let that = this
        const app = getApp();
	    app.globalData.switchId=true
        Request.post('/weChat/unbindIdentity?phone=' + that.data.phone, //调用方法
            {
                'content-type': 'application/json', // 默认值
            },
        ).then(res => { //成功回调
            if (res.data.success == true) {
                wx.navigateTo({
                  url: '/pages/index/index?showList=1&phone='+ that.data.phone,
                })
                wx.showToast({
                    title: '正在跳往切换页',
                    icon: 'none',
                    duration: 2000
                })
            }
        }).catch(err => { }) //异常回调
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.getXgAndHsData()
        setTimeout(() => {
            this.qr()
        }, 500)
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var timer= new Date()
        // 显示日期
        console.log(timer.getDate());
    },
    changeStr(str, index, changeStr) {
        return str.substr(0, index) + changeStr + str.substr(index + changeStr.length);
    },
    qr: function () {
        let that = this
        // dom节点查询器
        const query = wx.createSelectorQuery()
        query.select('#myQrcode')
            .fields({
                node: true,
                size: true
            })
            .exec((res) => {
                var canvas = res[0].node
                // 调用方法drawQrcode生成二维码
                drawQrcode({
                    canvas: canvas,
                    canvasId: 'myQrcode',
                    width: 400,
                    height: 400,
                    background: '#ffffff',
                    foreground: that.data.codeStr == '绿码' ? '#5bb06f' : that.data.codeStr == '黄码' ? '#e48826' : that.data.codeStr == '红码' ? '#e42926' : '#000',
                    text: that.data.encodeData,
                    callback(e) {
                        setTimeout(() => {
                            wx.canvasToTempFilePath({
                                canvas: canvas,
                                canvasId: 'myQrcode',
                                width: 400,
                                height: 400,
                                background: '#ffffff',
                                foreground: that.data.codeStr == '绿码' ? '#5bb06f' : that.data.codeStr == '黄码' ? '#e48826' : that.data.codeStr == '红码' ? '#e42926' : '#000',
                                text: that.data.encodeData,
                                success: (e) => {
                                    that.setData({ qrcodeImg: e.tempFilePath });
                                }
                            })
                        }, 500);
                    }
                })

            })

    },
    // 页面中没有，返回两个层级
    changePhone() {
        wx.navigateBack({
            delta: 2
        })
    },
    // 获取登录时间
    writeCurrentDate: function () {
        let now = new Date();
        let year = now.getFullYear(); //得到年份
        let month = now.getMonth() + 1; //得到月份
        let date = now.getDate(); //得到日期
        let day = now.getDay() //得到周几
        let hour = now.getHours(); //得到小时
        let minu = now.getMinutes(); //得到分钟
        let sec = now.getSeconds(); //得到秒
        let MS = now.getMilliseconds() //获取毫秒
        let time = "";
        time =
            (month < 10 ? "0" + month : month) +
            "月" +
            (date < 10 ? "0" + date : date) +
            "日 " +
            (hour < 10 ? "0" + hour : hour) +
            ":" +
            (minu < 10 ? "0" + minu : minu)
        this.setData({
            time: time
        })
    },
    // 跳转到核酸检测页
    nuc() {
        let data = encodeURIComponent(JSON.stringify(this.data.nucleicAcidInfos));
        wx.navigateTo({
            url: '../nuc/nuc?nucleicAcidInfos=' + data,
        })
    },
    vac() {
        let data = encodeURIComponent(JSON.stringify(this.data.vaccinationInfos));
        wx.navigateTo({
            url: '../vac/vac?vaccinationInfos=' + data,
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.setScreenBrightness(this.data.luminance)
        this.unbindIdentity()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },


})